﻿--#include "data\config\activityconfig\DailyActivity\TianHuangConfig.lua" once   --十二星宫
--#include "data\functions\Activity\DailyActivity\TianHuang.lua" once  

local npcId  = 144
local npcId2 = 145

NpcDialog[npcId]  = {}
NpcDialog[npcId2] = {}

local t  = NpcDialog[npcId]
local t2 = NpcDialog[npcId2]

t.main = function(sysarg)
	local circleLimit = TianHuangConfig.floors[1].enterLevelLimit[1]
	local levelLimit  = TianHuangConfig.floors[1].enterLevelLimit[2]
	local strColor1 = Lang.ScriptTips.Color_Greed
	if not Actor.checkActorLevel( sysarg, levelLimit, circleLimit ) then
		strColor1 = Lang.ScriptTips.Color_Red
	end

	local strColor3 	= Lang.ScriptTips.Color_Greed
	local needItemId 	= TianHuangConfig.floors[1].enterConsume[1].id
	local needItemCount = TianHuangConfig.floors[1].enterConsume[1].count
	local hasItemNum 	= Actor.getItemCount( sysarg, needItemId )
	if hasItemNum < needItemCount then
		strColor3 = Lang.ScriptTips.Color_Red
	end
	
	local openDays = System.getDaysSinceOpenServer()
	local strColor4 = Lang.ScriptTips.Color_Greed
	if openDays < TianHuangConfig.floors[1].openServerDay then
		strColor4 = Lang.ScriptTips.Color_Red
	end

	local needItemname	= Item.getItemName( needItemId ) or ""
	local strCond1 = string.format( Lang.ScriptTips.LevelLimitCircleFormat, strColor1, circleLimit, levelLimit )
	local strCond3 = string.format( Lang.ScriptTips.NeedItemFormat, strColor3, needItemname, needItemCount )
	local strCond4 = ""
	if TianHuangConfig.floors[1].openServerDay and openDays >= TianHuangConfig.floors[1].openServerDay then
		strCond4 = string.format( Lang.ScriptTips.LastOpenDaysLimitFormat2, strColor4  )
	else
		strCond4 = string.format( Lang.ScriptTips.LastOpenDaysLimitFormat1, strColor4, TianHuangConfig.floors[1].openServerDay-openDays )
	end
	--local strCond  = string.format("%s\n%s\n%s", strCond1, strCond3, strCond4 )
	local strCond  = string.format("%s\n%s", strCond1, strCond3 )
	-----------------
	local strDesc 		= ""
	local okBtn 		= ""			--{按钮;样式;按钮名;函数名;描述}
	local strBtnList 	= ""

	strDesc 	=  Lang.ScriptTips.TianHuang001
	okBtn 		= "{btn;0;"..Lang.ScriptTips.ButtonEnter..";%s;}"			--{按钮;样式;按钮名;函数名;描述}
	strBtnList 	= string.format(okBtn, "EnterTianHuangFromNpc")

	local pack = DataPack.allocPacket(sysarg, LogicCmd.npcDialogSys.sysID, LogicCmd.npcDialogSys.sub.sNpcDlgData)
	DataPack.writeUint64(pack, Actor.getNpc(sysarg))
	DataPack.writeByte(pack, NpcDialogType.FYSDNpcDlg)
	DataPack.writeString(pack, strCond)
	DataPack.writeString(pack, strDesc)
	DataPack.writeString(pack, strBtnList)
	DataPack.writeChar(pack, -1)
	DataPack.writeByte(pack, 0);
	DataPack.flush(pack)

	return ""
end

t2.main = function(sysarg)
	local floorCfg = GetTianHuangFloorCfgBySceneId( Actor.getSceneId( sysarg ) )
	if not floorCfg then 			--不在场景中，请先进入
		return
	end

	local nextFloorIdx = floorCfg.floorIdx + 1
	local nextFloorCfg = TianHuangConfig.floors[nextFloorIdx]
	if not nextFloorCfg then
		return
	end

	local circleLimit = nextFloorCfg.enterLevelLimit[1]
	local levelLimit  = nextFloorCfg.enterLevelLimit[2]
	local strColor1 = Lang.ScriptTips.Color_Greed
	if not Actor.checkActorLevel( sysarg, levelLimit, circleLimit ) then
		strColor1 = Lang.ScriptTips.Color_Red
	end

	local strColor3 	= Lang.ScriptTips.Color_Greed
	local needItemId 	= nextFloorCfg.enterConsume[1].id
	local needItemCount = nextFloorCfg.enterConsume[1].count
	local hasItemNum 	= Actor.getItemCount( sysarg, needItemId )
	if hasItemNum < needItemCount then
		strColor3 = Lang.ScriptTips.Color_Red
	end

	local openDays = System.getDaysSinceOpenServer()
	local strColor4 = Lang.ScriptTips.Color_Greed
	if openDays < nextFloorCfg.openServerDay then
		strColor4 = Lang.ScriptTips.Color_Red
	end

	local needItemname	= Item.getItemName( needItemId ) or ""
	local strCond1 = string.format( Lang.ScriptTips.LevelLimitCircleFormat, strColor1, circleLimit, levelLimit )
	local strCond3 = string.format( Lang.ScriptTips.NeedItemFormat, strColor3, needItemname, needItemCount )
	local strCond4 = ""
	if TianHuangConfig.floors[1].openServerDay and openDays >= nextFloorCfg.openServerDay then
		strCond4 = string.format( Lang.ScriptTips.LastOpenDaysLimitFormat2, strColor4  )
	else
		strCond4 = string.format( Lang.ScriptTips.LastOpenDaysLimitFormat1, strColor4, nextFloorCfg.openServerDay-openDays )
	end
	local strCond  = string.format("%s\n%s\n%s", strCond1, strCond3, strCond4 )
	-----------------
	local strDesc 		= ""
	local okBtn 		= ""			--{按钮;样式;按钮名;函数名;描述}
	local strBtnList 	= ""
	local sceneId 		= Actor.getSceneId( sysarg )
	local floorSts 		= TianHuangGetFloorSts( sceneId )
	if floorSts == 1 then
		local floorCfg 	= GetTianHuangFloorCfgBySceneId( Actor.getSceneId(sysarg) )		--当前楼层
		if not floorCfg then
			return
		end

		strDesc 		= Lang.ScriptTips.TianHuang001
		local okBtn1 	= "{btn;0;"..Lang.ScriptTips.ButtonEnterNext..";%s;}"			--{按钮;样式;按钮名;函数名;描述}
		local strBtn1 	= string.format(okBtn1, "NextTianHuangFromNpc")
		strBtnList	= strBtn1
	elseif floorSts == 2 then
		strDesc 	= Lang.ScriptTips.TianHuang001
		okBtn 		= "{btn;0;"..Lang.ScriptTips.ButtonExit..";%s;}"					--{按钮;样式;按钮名;函数名;描述}
		strBtnList 	= string.format(okBtn, "QuitTianHuangFromNpc")
	else
		return ""
	end

	
	local pack = DataPack.allocPacket(sysarg, LogicCmd.npcDialogSys.sysID, LogicCmd.npcDialogSys.sub.sNpcDlgData)
	
	DataPack.writeUint64(pack, Actor.getNpc(sysarg))
	DataPack.writeByte(pack, NpcDialogType.FYSDNpcDlg)
	DataPack.writeString(pack, strCond)
	DataPack.writeString(pack, strDesc)
	DataPack.writeString(pack, strBtnList)
	DataPack.writeChar(pack, -1)
	DataPack.writeByte(pack, 0);
	DataPack.flush(pack)

	return 
end

--进入天荒秘境
t.EnterTianHuangFromNpc = function ( sysarg )
	--print("EnterTwelvePalacesFromNpc...")
	Actor.closeNPCDialog(sysarg)
	EnterTianHuang( sysarg )
end

--进入天荒秘境下一层
t2.NextTianHuangFromNpc = function ( sysarg )
	--print("NextTwelvePalacesFromNpc...")
	Actor.closeNPCDialog(sysarg)
	NextTianHuang( sysarg )
end

--退出天荒秘境
t2.QuitTianHuangFromNpc = function ( sysarg )
	--print("QuitTianHuangFromNpc...")
	Actor.closeNPCDialog(sysarg)
	TianHuangQuit( sysarg )
end